﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace XKJ.OAWeb.Api.Filters
{
    //ResponseHeaderAttribute是一个自定义特性[ResponseHeaderAttribute]
    public class ResponseHeaderAttribute : ActionFilterAttribute
    {
        private readonly string _name;
        private readonly string _value;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public ResponseHeaderAttribute(string name, string value)
        {
            //元组赋值法
            (_name, _value) = (name, value);
        }
        /// <summary>
        /// 结果过滤器
        /// </summary>
        /// <param name="context"></param>
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            //添加响应头，键值对（需要了解HTTP协议）
            context.HttpContext.Response.Headers.Add(_name, _value);

            base.OnResultExecuting(context);
        }
    }
}
